package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author 吴老师
 * @since 2024-12-01
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {


    /**
     * 查询区域服务分页列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        //多表关联分页查询时，应该使用PageHelper工具类完成
        return PageHelperUtils.selectPage(dto,()->baseMapper.findServeListByRegionId(dto.getRegionId()));
    }

    @Autowired
    private IServeItemService serveItemService;

    @Autowired
    private IRegionService regionService;

    /**
     * 添加服务
     *
     * @param dtoList
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> dtoList) {

        for (ServeUpsertReqDTO dto : dtoList) {
            Long itemId = dto.getServeItemId();

            //1. 判断服务项是否存在
            ServeItem item = serveItemService.getById(itemId);
            if(item==null){
                throw new ForbiddenOperationException("服务项不存在");
            }

            //2. 判断服务项是否已启用
            if(item.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务项未启用");
            }

            //3. 判断服务项是否已添加过
            Integer count = this.lambdaQuery()
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if(count>0) {
                throw new ForbiddenOperationException("服务项已被添加过");
            }

            //4. 构建服务对象数据
            Serve serve = BeanUtils.copyBean(dto, Serve.class);
            Region region = regionService.getById(dto.getRegionId());
            serve.setCityCode(region.getCityCode()); //城市编码

            //5. 添加服务数据到表中
            boolean result = this.save(serve);
            if(!result) {
                throw new DBException("添加服务失败");
            }

        }
    }

    /**
     * 更改价格
     */
    @Override
    public void updatePrice(Long id, BigDecimal price) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if(serve==null) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //根据id更新价格
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if(!result) {
            throw new DBException("更新服务价格失败");
        }
    }

    /**
     * 服务上架
     */
    @Override
    public void onSale(Long id) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if(serve==null) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //判断是否有区域
        if(serve.getRegionId()==null){
            throw new ForbiddenOperationException("未选择区域");
        }
        //判断服务是否上架过
        if(serve.getSaleStatus()==FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务已上架");
        }
        //判断服务项是否存在
        ServeItem item = serveItemService.getById(serve.getServeItemId());
        if(item==null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        //判断服务是否已启用
        if(item.getActiveStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启用");
        }
        //根据id更新状态为已上架
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus,FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if(!result) {
            throw new DBException("服务上架失败");
        }
    }

    @Override
    public void offSale(Long id) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if(serve==null) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //判断服务项是否存在
        ServeItem item = serveItemService.getById(serve.getServeItemId());
        if(item==null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        //根据id更新状态为已下架，下架为1
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus,FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if(!result) {
            throw new DBException("服务上架失败");
        }
    }

    @Override
    public void onHot(Long id) {
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot,FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if(!result) {
            throw new DBException("服务设置热门失败");
        }
    }

    @Override
    public void offHot(Long id) {
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot,FoundationStatusEnum.INIT.getStatus())
                .update();
        if(!result) {
            throw new DBException("服务取消设置热门失败");
        }
    }
}
